package com.perfree.service;

import com.perfree.entity.Resource;
import com.perfree.mapper.ResourceMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ResourceService {

    private final ResourceMapper resourceMapper;

    public int insert(Resource resource) {
        resource.setId(null);
        return resourceMapper.insertSelective(resource);
    }

    public int insertBatch(List<Resource> list) {
        return resourceMapper.insertList(list);
    }

    public List<Resource> getResourceList() {
        return resourceMapper.selectAll();
    }

    public List<Resource> getResourceListByPid(Integer pid) {
        Example example = new Example(Resource.class);
        example.createCriteria().andEqualTo("pid",  pid);
        example.orderBy("sortNum").asc();
        return resourceMapper.selectByExample(example);
    }
}
